package cn.virens.library.uitls;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * 权限检查
 *
 * @author :loioi
 * @package :cn.loioi.util
 * @创建时间 :2016/3/26 9:15
 */
public class PermissionsHelper {
    /**
     * 判断权限集合
     *
     * @param permissions 检测权限的集合
     * @return 权限已全部获取返回true，未全部获取返回false
     */
    public static boolean checkPermissions(Context mContext, String... permissions) {
        for (String permission : permissions) if (!checkPermission(mContext, permission)) return false;

        return true;
    }

    /**
     * 判断权限是否获取
     *
     * @param permission 权限名称
     * @return 已授权返回true，未授权返回false
     */
    public static boolean checkPermission(Context mContext, String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_GRANTED;
    }


    /**
     * 获取权限
     *
     * @param resultCode
     * @param permission
     */
    public static boolean checkPermission(Context mContext, int resultCode, String permission) {
        if (!checkPermission(mContext, permission)) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext, permission)) {    // 因为requestPermissions方法会显示不在询问按钮
                showMissingPermissionDialog(mContext); //如果用户以前拒绝过改权限申请，则给用户提示
            } else {
                ActivityCompat.requestPermissions((Activity) mContext, new String[]{permission}, resultCode);    //进行权限请求
            }
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取权限
     *
     * @param resultCode
     * @param permissions
     */
    public static boolean checkPermission(Context mContext, int resultCode, String... permissions) {
        boolean result = true;
        for (String permission : permissions) result = result && checkPermission(mContext, resultCode, permission);

        return result;
    }

    // 显示缺失权限提示
    public static void showMissingPermissionDialog(final Context mContext) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final AlertDialog alertDialog = builder.create();
        builder.setMessage("当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。\n\n最后点击两次后退按钮，即可返回。");
        // 拒绝, 退出应用
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
        builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startAppSettings(mContext);
            }
        });
        builder.show();
    }

    // 启动应用的设置
    private static void startAppSettings(Context mContext) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + mContext.getPackageName()));
        mContext.startActivity(intent);
    }
}
